package com.sky.service.user.impl;

import com.sky.context.BaseContext;
import com.sky.exception.ShopIDNotNULLException;
import com.sky.mapper.user.ShoppingCartMapper;
import com.sky.mapper.user.UserMerchantMapper;
import com.sky.mapper.user.UserOrderMapper;
import com.sky.pojo.Merchant;
import com.sky.result.Result;
import com.sky.service.user.UserOrderService;
import com.sky.utils.MapUtils;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class UserOrderServiceImpl implements UserOrderService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private UserMerchantMapper userMerchantMapper;

    @Override
    public Result<String> getDeliveryTime(Integer shopId, String customerAddress) {
        //获取所有菜品制作的时间
        Long currentId = BaseContext.getCurrentId();
        Integer makeTimeCost = shoppingCartMapper.getMakeTimeByUserId(currentId);
        //计算配送地址和商家地址之间的配送时间
        Merchant merchant = userMerchantMapper.selectById(shopId);
        if (merchant == null) {
            throw new ShopIDNotNULLException("找不到商家信息");
        }
        //工具类，直接使用
        Integer towPointCost = MapUtils.baoiduTwoPointCostTime(merchant.getShopAddress(), customerAddress);

        //预计送达时间 = 当前时间+菜品制作时间+配送时间+预留10分钟
        Integer allCost = makeTimeCost * 60 + towPointCost + 600;
        LocalDateTime localDateTime = LocalDateTime.now().plusSeconds(allCost);
        return Result.success(localDateTime.toString());
    }
}
